﻿using Authorization.Center.Controllers;
using Authorization.Center.SqlServer;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using S.Common.Enums.Initial;
using S.Common.Enums.PregnancyTest;
using S.Common.helper;
using S.Initial.Domain;
using S.Initial.Infrastructure.Dto;
using S.Initial.Read.API.Application.Command.BreedingMothers;
using S.Initial.Read.API.Controllers.Application.Command.BreedingMothers;
using Yitter.IdGenerator;

namespace S.Initial.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InitialController : BaseController
    {
        private readonly IMediator mediator;
        private readonly IConfiguration configuration;
        private readonly IHttpContextAccessor httpContextAccessor;
        public InitialController(IMediator _mediator, MySqlDbContext db,IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(db)
        {
            mediator = _mediator;
            this.configuration = configuration;
            this.httpContextAccessor = httpContextAccessor;
        }

        #region 雪花id
        /// <summary>
        /// 雪花id
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public long XID()
        {
            return YitIdHelper.NextId();
        }
        #endregion

        #region 期初
        /// <summary>
        /// 期初表显示
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> AllBreedingMothers(ShowBreedingMothersCommand t)
        {
            var result = await mediator.Send(t);
            var items = result.items;
            var totalCount = result.totalCount;
            var pageCount = result.pageCount;
            // 返回分页结果
            return Ok(new { items, totalCount, pageCount });
        }
        /// <summary>
        /// 枚举阶段
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult StageEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<Stage>())
            {
                list.Add(new
                {
                    name = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }
        #endregion

        #region 栋舍下拉框显示
        /// <summary>
        /// 跨服务请求栋舍
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetFieldModel()
        {
            var jsonString = HttpClientHelper.GetResponse($"{configuration.GetValue<string>("Gateway")}/Authorization/api/ColumnManagement/GetColumnList");
            var response = JsonConvert.DeserializeObject<List<SelectDto>>(jsonString);
            return Ok(response);
        }

        /// <summary>
        /// 跨服务请求栏位
        /// </summary>
        /// <returns></returns>
        /// 
        [Authorize]
        [HttpGet]
        public IActionResult GetHouseField(int id)
        {
            var jsonString = HttpClientHelper.GetResponse($"{configuration.GetValue<string>("Gateway")}/Authorizationapi/ColumnManagement/GetHouseField?id={id}");
            var response = JsonConvert.DeserializeObject<List<SelectDto>>(jsonString);
            return Ok(response);
        }

        /// <summary>
        /// 跨服务请求负责人信息
        /// </summary>
        /// <returns></returns>
        /// 
        [Authorize]
        [HttpGet]
        public IActionResult GetUser()
        {
            var jsonString = HttpClientHelper.GetResponse($"{configuration.GetValue<string>("Gateway")}/Authorization/api/UserRead/GetUser");
            // 反序列化并显示栋舍数据
            var response = JsonConvert.DeserializeObject<List<SelectDto>>(jsonString);
            return Ok(response);
        }
        #endregion
        
        #region 耳号（期初）
        /// <summary>
        /// 跨服务请求耳号
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GeteiTag(int id)
        {
            var jsonString = HttpClientHelper.GetResponse($"{configuration.GetValue<string>("Gateway")}/ach/api/Provide/SelMaleEmaleSel?id={id}");
            var response = JsonConvert.DeserializeObject<List<ESelectDto>>(jsonString);
            return Ok(response);
        }
        #endregion

        #region 存栏
        /// <summary>
        /// 存栏
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IEnumerable<InventoryStatisticsDto>> ShowInventoryStatistics(ShowInventoryStatisticsCommand t)
        {
            return await mediator.Send(t);
        }
        #endregion

        #region 查询条件的值
        /// <summary>
        /// 查询条件的值
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IEnumerable<InventoryStatisticsDto>> ShowChaZhiStatistics(ShowChaZhiCommand t)
        {
            return await mediator.Send(t);
        }
        #endregion

        #region 读取配置文件
        /// <summary>
        /// 测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TestGet()
        {
            var cfRt = configuration.GetValue<string>("Consul");//读取Consul路由
            return Ok(cfRt);
        }
        #endregion

        #region 类
        public class SelectDto
        {
            /// <summary>
            /// 编号
            /// </summary>
            public int value { get; set; }
            /// <summary>
            /// 名称
            /// </summary>
            public string? label { get; set; }
        }
        public class ESelectDto
        {
            /// <summary>
            /// 编号
            /// </summary>
            public string? value { get; set; }
            /// <summary>
            /// 名称
            /// </summary>
            public string? label { get; set; }
        }

        #endregion

    }
}
